package com.giggle.nacos.registry.springcloud;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.giggle.common.helper.ReflectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;

/**
 * @author guozichen
 * @ClassName:
 * @Description: (这里用一句话描述累的作用)
 * @date 2020/8/26 17:51
 */
public class NacosRegistration extends com.alibaba.cloud.nacos.registry.NacosRegistration {
    private static final Logger logger = LoggerFactory.getLogger(NacosRegistration.class);

    public NacosRegistration(NacosDiscoveryProperties nacosDiscoveryProperties, ApplicationContext context) {
        super(nacosDiscoveryProperties, context);
    }

    @PostConstruct
    public void init() {
        super.init();
        Boolean registerEnabled = true;
        Environment environment = this.getApplicationContext().getEnvironment();
        if (environment != null) {
            registerEnabled = Boolean.valueOf(environment.getProperty("spring.cloud.nacos.discovery.register-enabled", "true"));
        }

        if (registerEnabled) {
            Map<String, String> metadata = this.getNacosProperties().getMetadata();
            Map<String, Object> restMap = this.getApplicationContext().getBeansWithAnnotation(RestController.class);
            StringBuilder restServicePathSb = new StringBuilder();
            Iterator var6 = restMap.entrySet().iterator();

            while(true) {
                Class restClass;
                do {
                    if (!var6.hasNext()) {
                        if (restServicePathSb.length() > 0) {
                            metadata.put("RestFull Service List", restServicePathSb.toString());
                        }

                        return;
                    }

                    Map.Entry<String, Object> entry = (Map.Entry)var6.next();
                    restClass = entry.getValue().getClass();
                } while(!restClass.isAnnotationPresent(RequestMapping.class));

                RequestMapping classAnnotation = (RequestMapping)restClass.getAnnotation(RequestMapping.class);
                String[] classValueArray = classAnnotation.value();
                String[] var11 = classValueArray;
                int var12 = classValueArray.length;

                for(int var13 = 0; var13 < var12; ++var13) {
                    String classValue = var11[var13];
                    Method[] methodArray = restClass.getMethods();
                    Method[] var16 = methodArray;
                    int var17 = methodArray.length;

                    for(int var18 = 0; var18 < var17; ++var18) {
                        Method method = var16[var18];
                        String[] methodValueArray = null;
                        if (method.isAnnotationPresent(RequestMapping.class)) {
                            RequestMapping annotation = (RequestMapping)method.getAnnotation(RequestMapping.class);
                            methodValueArray = annotation.value();
                        }

                        if (method.isAnnotationPresent(PostMapping.class)) {
                            PostMapping annotation = (PostMapping)method.getAnnotation(PostMapping.class);
                            methodValueArray = annotation.value();
                        }

                        if (method.isAnnotationPresent(GetMapping.class)) {
                            GetMapping annotation = (GetMapping)method.getAnnotation(GetMapping.class);
                            methodValueArray = annotation.value();
                        }

                        if (methodValueArray != null) {
                            String[] var27 = methodValueArray;
                            int var22 = methodValueArray.length;

                            for(int var23 = 0; var23 < var22; ++var23) {
                                String methodValue = var27[var23];
                                if (restServicePathSb.length() > 0) {
                                    restServicePathSb.append(",");
                                }

                                restServicePathSb.append(classValue).append(methodValue);
                            }
                        }
                    }
                }
            }
        }
    }

    private NacosDiscoveryProperties getNacosProperties() {
        NacosDiscoveryProperties discoveryProperties = (NacosDiscoveryProperties)ReflectHelper.getFieldValue(this, "nacosDiscoveryProperties");
        return discoveryProperties;
    }

    private ApplicationContext getApplicationContext() {
        ApplicationContext context = (ApplicationContext)ReflectHelper.getFieldValue(this, "context");
        return context;
    }
}

